# -*- coding: utf-8 -*-
# @Time    : 2020/8/23 下午11:25
# @Author  : lilong

"""
基于pipeline训练结束后，进行nlu模型构建解释器，也就是预测。
"""

import os
import asyncio
from rasa.nlu.utils import json_to_string

from rasa.cli.utils import print_success
from rasa.nlu.model import Interpreter
from rasa.core.interpreter import INTENT_MESSAGE_PREFIX, RegexInterpreter

# # 自己解压放到指定位置
model_path = "../bot_playground/bot_tiny/models/nlu/nlu-20210807-163219/nlu"

# # 解压到临时文件夹
# model_path = "../bot_playground/bot_tiny/models/nlu"
# 解压文件
# from rasa.model import get_model
# tmp_model_path = get_model(model_path)
# interpreter = Interpreter.load(os.path.join(tmp_model_path, 'nlu'), None)

interpreter = Interpreter.load(model_path, None)
regex_interpreter = RegexInterpreter()

print_success("NLU model loaded. Type a message and press enter to parse it.")
while True:
    print_success("Next message:")
    # message = input().strip()

    # message = "/我想去旅游"
    message = "我想去旅游"

    if message.startswith(INTENT_MESSAGE_PREFIX):
        loop = asyncio.get_event_loop()
        result = loop.run_until_complete(regex_interpreter.parse(message))  # CORE模块的正则预测
    else:
        result = interpreter.parse(message)  # NLU模块的model模型预测

    print(json_to_string(result))

    break
